﻿using System;
//using MainJumpFleet.Loaders;

// XNA lib
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

//Oak Engine Lib
using OakEngine.GameState;
using OakEngine.Input;
using Microsoft.Xna.Framework.Input;
using MainJumpFleet.TempObjects;
using JumpFleetGameLib.UI;
using OakEngine.Camera;


namespace MainJumpFleet.GameStates
{
    public partial class PrototypePlaygroundScreenState : OAK_IGameState
    {
        SpriteBatch batch;
        SpriteFont mainMenuFont;
        Random r;

        ///////////////////////////////////
        // Player Ship MOvement
        ship playerShip;
        Vector2 forwardVector;
        float speed;
        float acceleration;
        float dt;


        /*********************************/
        /********************************
         * Enemy Ships
         ********************************/
        TempObjects.EnemyShip[] enemy;
        Texture2D dot;
        Vector2 dotPosition = Vector2.Zero;




        /**********************************/

        HUD hud;
        //
        Oak_Camera cam;

        public int ScreenWidth;
        public int ScreenHeight;

        private JumpFleetGameLib.xSprite.SpriteManager spriteManager;

        private Texture2D starfieldBackgroundTexture;
        private Texture2D playerShipTexture;
        private Texture2D bulletTexture;
        private Texture2D enemyShipFighterTexture;

        private JumpFleetGameLib.xMap.SectorMap.SectorMap prototypeSectorMap;

        private Song backgroundMusicSong;
        private bool isBackgroundMusicPaused;

        public PrototypePlaygroundScreenState()
        {
            Enter();
        }


        public void Enter()
        {
            ScreenWidth = CGame.CGame.GetInstance().Graphics.PreferredBackBufferWidth;
            ScreenHeight = CGame.CGame.GetInstance().Graphics.PreferredBackBufferHeight;

            mainMenuFont = CGame.CGame.GetInstance().Content.Load<SpriteFont>("Fonts\\mainMenuFont");
            batch = CGame.CGame.GetInstance().SpriteBatch;
            dot = CGame.CGame.GetInstance().Content.Load<Texture2D>("Debug\\dot");
            r = new Random();

            hud = new HUD(CGame.CGame.GetInstance().Content);

            cam = new Oak_Camera(Vector2.Zero, ScreenWidth, ScreenHeight, 3072, 3072, 100.0f);

            spriteManager = new JumpFleetGameLib.xSprite.SpriteManager(ref cam);

            LoadAssets();

            ///////////////////////
            // Ship Player
            ///////////////////////
            playerShip = new ship(spriteManager.CreateNewStaticSpriteFromTemplate("PlayerShip"), mainMenuFont, ref cam);


            playerShip.Visible = true;
            playerShip.Position = new Vector2(500, 300);
            forwardVector = new Vector2(1, 0);
            speed = 0;
            acceleration = 100.0f;


            /********************************
               * Enemy Ships
            ********************************/
            enemy = new TempObjects.EnemyShip[1000];

            for (int i = 0; i < enemy.Length; i++)
            {
                enemy[i] = new TempObjects.EnemyShip(1, spriteManager.CreateNewStaticSpriteFromTemplate("EnemyFighter"), spriteManager.CreateNewStaticSpriteFromTemplate("Bullet"), new Vector2((float)r.Next(0, 5000), (float)r.Next(0, 5000)), dotPosition, ref cam);
                enemy[i].Visible = true;
            }
        }

        public bool Input(OAK_InputManager input)
        {

            if (input.Pressed(Keys.Escape) || input.Pressed(Buttons.Back, 0))
                CGame.CGame.GetInstance().ChangeState(new MainMenuState());

            if (input.Pressed(Keys.P))
            {
                MediaPlayer.Pause();
                isBackgroundMusicPaused = true;
            }

            if (input.Pressed(Keys.O))
            {
                MediaPlayer.Resume();
                isBackgroundMusicPaused = false;
            }


            Vector2 dir = new Vector2(0, 0);

            if (speed <= -150.0f)
                speed = -150.0f;

            if (input.LeftTriggerPosition(0) > 0.0f)
            {
                speed -= acceleration * dt;
            }
            else
            {
                if (speed <= 0)
                    speed += 5.0f;
            }
            //if (input.LeftStickPosition(0).Y > 0.0f)
            //{
            //    speed += acceleration * dt;

            //}
            if (input.LeftStickPosition(0).X < 0.0f)
            {
                playerShip.SetLeftRotation(dt);
            }
            if (input.LeftStickPosition(0).X > 0.0f)
            {
                playerShip.SetRightRotation(dt);

            }

            dir.Y = speed;
            dir = OakEngine.AI.Tools.OAK_AITools.Vector2DRotate(dir, playerShip.Rotation);
            playerShip.dir = OakEngine.AI.Tools.OAK_AITools.Vector2DRotate(new Vector2(0, -1), playerShip.Rotation); ;
            playerShip.velocity = dir;

            if (input.Pressed(Buttons.A, 0))
                playerShip.Fire(spriteManager.CreateNewStaticSpriteFromTemplate("Bullet"), playerShip.dir);



            return false;
        }
        public void Update(GameTime dt)
        {
            this.dt = (float)dt.ElapsedGameTime.TotalSeconds;

            // Camera Update
            cam.Update(dt);

            // Hud Update
            hud.UpdateHUD(dt);

            // ship
            playerShip.Update(this.dt);

            // Enemy Update

            for (int i = 0; i < enemy.Length; i++)
            {
                enemy[i].SetTarget(playerShip.Position);
                enemy[i].SetTargetBound(ref playerShip.Bounds);
                enemy[i].Update(dt);
            }
        }

        public void Render()
        {
            spriteManager.CameraDraw(batch);


            // playerShip.Draw(batch);

            /*
            for (int i = 0; i < enemy.Length; i++)
            {
                enemy[i].Render(batch, ref cam);

            }
            */

            hud.DrawHUD(batch);
        }

        public void Exit()
        {
            MediaPlayer.Stop();
        }
    }
}
